import { MongoClient } from 'mongodb';
import * as dotenv from 'dotenv';

dotenv.config();

const testTemplates = [
    {
        name: "动漫人物生成",
        description: "生成高质量的动漫风格人物立绘，支持自定义服装、发型和表情。",
        category: "人物",
        workflow: {
            nodes: {
                "1": {
                    "type": "KSampler",
                    "inputs": {
                        "seed": 123456789,
                        "steps": 20,
                        "cfg": 7,
                        "sampler_name": "euler_ancestral",
                        "scheduler": "normal",
                        "denoise": 1,
                        "model": ["4", 0],
                        "positive": ["6", 0],
                        "negative": ["7", 0],
                        "latent_image": ["3", 0]
                    }
                }
            }
        },
        tags: ["动漫", "人物", "立绘"],
        thumbnail: "/uploads/thumbnails/anime-1.jpg",
        author: "AI画师",
        likes: 128,
        views: 1024,
        isActive: true,
        defaultParams: {
            prompt: "beautiful anime girl, school uniform, happy, detailed face",
            negative_prompt: "bad quality, low quality",
            steps: 20,
            cfg_scale: 7,
            width: 512,
            height: 768
        },
        previewImages: [
            "/uploads/previews/anime-1.jpg",
            "/uploads/previews/anime-2.jpg",
            "/uploads/previews/anime-3.jpg"
        ],
        metadata: {
            modelVersion: "AnythingV5",
            difficulty: "简单",
            estimatedTime: 15
        }
    },
    {
        name: "风景场景生成",
        description: "生成精美的风景场景图，包括自然风光和城市景观。",
        category: "场景",
        workflow: {
            nodes: {
                // 简化的工作流配置
                "1": {
                    "type": "KSampler",
                    "inputs": {
                        "seed": 987654321,
                        "steps": 30,
                        "cfg": 7.5,
                        "sampler_name": "euler",
                        "scheduler": "karras",
                        "denoise": 1,
                        "model": ["4", 0],
                        "positive": ["6", 0],
                        "negative": ["7", 0],
                        "latent_image": ["3", 0]
                    }
                }
            }
        },
        tags: ["风景", "自然", "城市"],
        thumbnail: "/uploads/thumbnails/landscape-1.jpg",
        author: "AI画师",
        likes: 256,
        views: 2048,
        isActive: true,
        defaultParams: {
            prompt: "beautiful landscape, sunset, mountains, detailed",
            negative_prompt: "bad quality, blurry",
            steps: 30,
            cfg_scale: 7.5,
            width: 768,
            height: 512
        },
        previewImages: [
            "/uploads/previews/landscape-1.jpg",
            "/uploads/previews/landscape-2.jpg"
        ],
        metadata: {
            modelVersion: "SDXL",
            difficulty: "中等",
            estimatedTime: 20
        }
    },
    {
        name: "概念艺术生成",
        description: "生成独特的概念艺术作品，适合游戏和电影项目使用。",
        category: "艺术",
        workflow: {
            nodes: {
                "1": {
                    "type": "KSampler",
                    "inputs": {
                        "seed": 135792468,
                        "steps": 40,
                        "cfg": 8,
                        "sampler_name": "dpmpp_2m",
                        "scheduler": "karras",
                        "denoise": 1,
                        "model": ["4", 0],
                        "positive": ["6", 0],
                        "negative": ["7", 0],
                        "latent_image": ["3", 0]
                    }
                }
            }
        },
        tags: ["概念艺术", "游戏", "电影"],
        thumbnail: "/uploads/thumbnails/concept-1.jpg",
        author: "AI艺术家",
        likes: 512,
        views: 4096,
        isActive: true,
        defaultParams: {
            prompt: "sci-fi concept art, futuristic city, detailed architecture",
            negative_prompt: "bad quality, simple, lack of details",
            steps: 40,
            cfg_scale: 8,
            width: 896,
            height: 512
        },
        previewImages: [
            "/uploads/previews/concept-1.jpg",
            "/uploads/previews/concept-2.jpg",
            "/uploads/previews/concept-3.jpg"
        ],
        metadata: {
            modelVersion: "DreamShaper",
            difficulty: "高级",
            estimatedTime: 30
        }
    }
];

async function initDb() {
    const client = new MongoClient(process.env.MONGODB_URI || 'mongodb://localhost:27017/ai_drawing');

    try {
        await client.connect();
        console.log('Connected to MongoDB');

        const db = client.db();
        const collection = db.collection('workflow_templates');

        // 清空现有数据
        await collection.deleteMany({});
        console.log('Cleared existing data');

        // 插入测试数据
        const result = await collection.insertMany(testTemplates);
        console.log(`Inserted ${result.insertedCount} templates`);

        // 创建索引
        await collection.createIndex({ name: 'text', description: 'text', tags: 'text' });
        await collection.createIndex({ category: 1 });
        await collection.createIndex({ createdAt: -1 });
        await collection.createIndex({ author: 1 });
        await collection.createIndex({ isActive: 1 });
        console.log('Created indexes');

    } catch (error) {
        console.error('Error initializing database:', error);
    } finally {
        await client.close();
        console.log('Database connection closed');
    }
}

// 运行初始化脚本
initDb().catch(console.error);